package com.yami.shop.common.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class RequestLogFilter implements Filter {

    private static final Logger log = LoggerFactory.getLogger(RequestLogFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;

        // 记录请求信息
        log.info("Request: {} {} from IP={}",
                httpRequest.getMethod(),
                httpRequest.getRequestURI(),
                httpRequest.getRemoteAddr());

        // 继续执行后续 Filter 或 Controller
        chain.doFilter(request, response);

        // 记录响应信息（如果需要记录响应数据，需使用 HttpServletResponseWrapper）
        log.info("Response: {} {} - Completed",
                httpRequest.getMethod(),
                httpRequest.getRequestURI());
    }
}
